今天我們要來介紹tabbar,首先我們要到檔案中新增一個TabBarViewController,
預設的tabbar是在底下的所以我們沒辦法移動他的位子,所以我們設定我們需要的東西
private func setupTabs() {
let wordtime = createNav(whit: "世界時間", and: UIImage(systemName: "house"), vc: 世界時間())
let home = createNav(whit: "鬧鐘", and: UIImage(systemName: "house"), vc: 鬧鐘())
let seconds = createNav(whit: "碼表", and: UIImage(systemName: "house"), vc: 碼表())
let timepiece = createNav(whit: "計時器", and: UIImage(systemName: "house"), vc: 計時器())
self.setViewControllers([wordtime,home,seconds,timepiece], animated: true)
}
private func createNav(whit title: String, and image:UIImage?, vc:UIViewController) -> UINavigationController {
let nav = UINavigationController(rootViewController: vc)
nav.tabBarItem.title = title
nav.tabBarItem.image = image
return nav
}
加上我們這兩行外我們要到SceneDelegate裡加上讓我們一開始就加載tabbar
let window = UIWindow(windowScene: windowScene)
window.rootViewController = TabBarViewController()
self.window = window
self.window?.makeKeyAndVisible()
我們這樣就可以正常顯示tabbar摟